﻿
using Models.Common;
using Models.Device;
using Newtonsoft.Json;
using static Models.Common.EnumDeviceState;
using UtilsBase;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    /// 空调红外遥控器RS584
    /// </summary>
    public class ServiceDeviceRS4851035_HW
    {

        //回调获获状态,为了避免误差
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;

                int status = 1;// int.Parse(httpReqModel.respCode);
                string key = "";
                if (httpReqModel.respCode == ResponseCode.SUCCESS)
                {
                    ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
                    ModelCommandAgreementParameter modelCommandAgreementParameter = UtilsObject.DynamicToEntity<ModelCommandAgreementParameter>(modelCommandAgreement.agreementParameter);
                    List<ModelCommandAgreementParameterCommand> commands = UtilsObject.DynamicToEntity<List<ModelCommandAgreementParameterCommand>>(modelCommandAgreement.agreementParameter.command);
                    ModelCommandAgreementParameterCommand command = commands.Where(c => c.key == commandLogModel.key).FirstOrDefault();


                    if (string.IsNullOrWhiteSpace(modelDeviceData.commandResults[0].value))
                    {//如果是空，无响应
                        return;
                    }
                    string result = modelDeviceData.commandResults[0].value;
                    if (commandLogModel.key == "open")
                    {
                        //开
                        key = "open";
                        log.writeSensorlog(commandLogModel.d_guid, "status", key, commandLogModel.name);
                    }
                    else if (commandLogModel.key == "close")
                    {
                        //关
                        key = "close";
                        log.writeSensorlog(commandLogModel.d_guid, "status", key, commandLogModel.name);
                    }
                    else if (commandLogModel.key == "hot")
                    {
                        //制热
                        key = "hot";
                        log.writeSensorlog(commandLogModel.d_guid, "status", key, commandLogModel.name);
                    }
                    else if (commandLogModel.key == "cold")
                    {
                        //热冷
                        key = "cold";
                        log.writeSensorlog(commandLogModel.d_guid, "status", key, commandLogModel.name);
                    }

                }
                //else
                //{
                //    status = (int)Device_State.error;
                //    //更新设备状态，设备状态异常
                //    new ServiceCloudDeviceLiveState().setDeviceLiveState(commandLogModel.d_guid, status, httpReqModel.respMsg);
                //}

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
